package com.borun.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {

	//这里可以设置你们自己数据库名称
	// private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=WordBase";
	// private static final String USER = "sa";
	// private static final String PASSWORD = "123456";
	
	
	// 这里是链接我们实验室服务上数据库
    private final static String URL = "jdbc:sqlserver://192.168.219.55:1433;DatabaseName=WordBase";
	private static final String USER = "sa";
	private static final String PASSWORD = "Thanks2015";
	
	

	private static Connection conn = null;
	// 静态代码块（将加载驱动、连接数据库放入静态块中）
	static {
		try {
			// 1.加载驱动程序
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			// 2.获得数据库的连接
			conn = (Connection) DriverManager
					.getConnection(URL, USER, PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// 对外提供一个方法来获取数据库连接
	public static Connection getConnection() {
		return conn;
	}

	public static void colseConnection() {
		try {
			if (conn != null && !conn.isClosed())
				conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 测试用例
	public static void main(String[] args) throws Exception {

		// 3.通过数据库的连接操作数据库，实现增删改查
		Statement stmt = conn.createStatement();
		// ResultSet executeQuery(String sqlString)：执行查询数据库的SQL语句
		// ，返回一个结果集（ResultSet）对象。
		ResultSet rs = stmt
				.executeQuery("select userid,username,password from t_user");
		while (rs.next()) {// 如果对象中有数据，就会循环打印出来
			System.out
					.println(rs.getInt("userid") + ","
							+ rs.getString("username") + ","
							+ rs.getString("password"));
		}
	}

}